package com.cabrida.livestar.guide;

import java.util.Timer;
import java.util.TimerTask;

import org.apache.http.Header;
import org.json.JSONObject;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

import cn.jpush.android.api.JPushInterface;

import com.cabrida.livestar.BaseActivity;
import com.cabrida.livestar.R;
import com.cabrida.livestar.config.Global;
import com.cabrida.livestar.config.HttpHelp;
import com.cabrida.livestar.config.Urls;
import com.cabrida.livestar.login.LoginActivity;
import com.cabrida.livestar.main.MainActivity;
import com.cabrida.livestar.mainstar.MainStarActivity;
import com.cabrida.livestar.model.Update;
import com.cabrida.livestar.receiver.ExampleUtil;
import com.cabrida.livestar.util.HttpUtils;
import com.cabrida.livestar.util.JsonUtils;
import com.cabrida.livestar.util.L;
import com.cabrida.livestar.util.NetworkUtils;
import com.cabrida.livestar.util.SharedUtils;
import com.cabrida.livestar.util.UpdataUtil;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class GuideActivity extends BaseActivity {
	public static final String TAG = GuideActivity.class.getSimpleName();
	private Timer timer ;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);
		DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
		Global.Screen_width = displaymetrics.widthPixels;
		Global.Screen_height = displaymetrics.heightPixels;
		
		
		
		
		String udid =  ExampleUtil.getImei(getApplicationContext(), "");
        
		String appKey = ExampleUtil.getAppKey(getApplicationContext());
		if (null == appKey) appKey = "AppKey异常";

		String packageName =  getPackageName();

		String deviceId = ExampleUtil.getDeviceId(getApplicationContext());

		String rid = JPushInterface.getRegistrationID(getApplicationContext());
		
		String versionName =  ExampleUtil.GetVersion(getApplicationContext());
		
		ImageView imageView = (ImageView) findViewById(R.id.imageView);
		imageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				clickBtn();
			}
		});

		checkVersion();
		
		
		timer = new Timer();
		TimerTask task = new TimerTask() {
			@Override
			public void run() {
				clickBtn();
			}
		};
		timer.schedule(task, 3000);
	}
	
	private void clickBtn(){
		timer.cancel();
		String U_token = SharedUtils.getString(context, "U_token");
		
		if (TextUtils.isEmpty(U_token)) {
			openActivity(LoginActivity.class);
		} else {
			Global.U_id = SharedUtils.getString(context, "U_id");
			Global.S_name = SharedUtils.getString(context, "S_name");
			Global.U_nickname = SharedUtils.getString(context, "U_nickname");
			Global.U_balance = SharedUtils.getString(context, "U_balance");
			Global.U_type = SharedUtils.getString(context, "U_type");
			Global.U_token = SharedUtils.getString(context, "U_token");
			Global.TIm_Sig = SharedUtils.getString(context, "TIm_Sig");
			Global.headPic = SharedUtils.getString(context, "headPic");
			Global.U_sex = SharedUtils.getString(context, "U_sex");
			Global.U_age = SharedUtils.getString(context, "U_age");
			Global.U_city = SharedUtils.getString(context, "U_city");
			if (Global.U_type.equals(Global.USER_TYPE_STAR)) {
				openActivity(MainStarActivity.class);
			} else {
				openActivity(MainActivity.class);
			}
		}
		finish();
	
	}
	
	private void checkVersion() {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("type", "20");

		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.USER_CHECKVERSION_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "checkVersion", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);

						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response,
									"data", new JSONObject());
							String v_number = JsonUtils.getString(data,"v_number", "");
							String v_description = JsonUtils.getString(data,"v_description", "");
							String v_digital = JsonUtils.getString(data,"v_digital", "");
							String v_link = JsonUtils.getString(data,"v_link", "");
							if (v_number.equals(Urls.SERVERS_VERSIONS)) {
								
							}else {
								//更新
								Update mUpdate = new Update();
								mUpdate.setDownloadUrl(v_link);
								mUpdate.setUpdateLog(v_description);
								mUpdate.setVersionCode(v_digital);
								mUpdate.setVersionName(v_number);
								UpdataUtil.getUpdateManager().showNoticeDialog(context,mUpdate);
							}
						} else {
							JSONObject data = JsonUtils.getJSONObject(response,
									"data", new JSONObject());
							String message = JsonUtils.getString(data,
									"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}


	@Override
	protected void findViewById() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub

	}

}
